package cn.im.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.im.util.ResultVoUtil;
import cn.im.view.ListUserForm;
import cn.im.view.ResultVo;
import cn.im.view.UserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.im.domain.User;
import cn.im.service.IUserService;
import cn.im.util.JacksonUtil;

/**
 * 结合项目实际需要，类RESTful风格： 
 * 1.请求方法调整：只使用GET、POST
 * 2.返回报文调整：使用josn格式，{"errcode":40003,"errmsg":"xxx"}来代替状态码 
 * 3.在URI标识中，增加了动词
 * 
 * @author Administrator
 *
 */
@Api(tags = "user api")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Autowired
	IUserService userService;


	@Autowired
	private StringRedisTemplate redisTemplate;

	@RequestMapping(value = "/total", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK) // 标识接口正常返回时的HTTP状态码
	public Object redisTest(@RequestParam("num") int num) {

		log.info("nums : {}",redisTemplate.opsForValue().get("10000"));
		Long nums = Long.valueOf(redisTemplate.opsForValue().get("10000"));
		nums = redisTemplate.opsForValue().increment("10000",num);
		return nums;
	}



	@ApiOperation(value = "获取用户信息", notes = "根据id获取用户信息")
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK) // 标识接口正常返回时的HTTP状态码
	public Object getUser(@PathVariable("id") String id) {
		return userService.getUserById(id);
	}

	/**
	 * 查询所有用户
	 * curl "http://127.0.0.1:8081/api/user/all"
	 * @return
	 */
	@ApiOperation(value = "获取用户列表", notes = "获取所有用户列表")
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	@ApiResponses(@ApiResponse(code = 200,message = "success",response = User.class))
	public List<User> getUsers() {
		return userService.getUserList();
	}

	/**
	 * 新增用户，对象类型转换(form-->entity)，入库保存
	 * curl -X POST -d '{xx:"xx"}'  http://127.0.0.1:8081/api/user/add
	 * @param userForm
	 * @return
	 */
	@ApiOperation(value = "添加用户", notes = "添加用户")
	@ApiImplicitParam(name="user" , value="用户实体user",dataType="UserVo",required=true)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Object addUser(@RequestBody UserVo userForm) {
		return userForm != null ? userService.addUser(userForm.buildEntity()) : null;
	}

	@ApiOperation(value = "更新用户", notes = "更新用户")
	@RequestMapping(value = "/update", method = RequestMethod.POST ,consumes="application/json")
	public Object updateUser(@RequestParam("id") String id, @RequestBody User user) {
		userService.updateUser(id, user);
		return ResultVoUtil.success();

	}

	@ApiOperation(value = "删除用户", notes = "删除用户")
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Object delUser(@RequestParam(value = "id", required = true) String id,@RequestBody Map<String,String> map) {
		System.out.println(map.keySet().toString());
		return userService.delUser(id)?ResultVoUtil.success():ResultVoUtil.error(40001, "failed");
	}


	/**
	 * 获取用户列表
	 * @param listUserForm 表单数据
	 * @return 用户列表
	 */
	@ApiOperation("获取用户列表")
	@GetMapping("/listUser")
	@ApiResponses(
			@ApiResponse(code = 200, message = "操作成功", response = UserVo.class)
	)
	public ResultVo listUser(@Validated ListUserForm listUserForm){
		return ResultVoUtil.success(userService.listUser(listUserForm));
	}




}
